package com.zpark.service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zpark.mapper.UserMapper;
import com.zpark.pojo.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import java.util.List;

@Service
public class UserService implements UserServiceImpl{
    @Autowired
    private UserMapper userMapper;

    @Override
    public User findById(Integer id) {
        return userMapper.selectByPrimaryKey(id);
    }

    @Override
    public Integer removeById(Integer id) {
        User user = new User();
        user.setUid(id);
        return userMapper.deleteByPrimaryKey(id);
    }

    @Override
    public int changById(User user) {
        return userMapper.updateByPrimaryKey(user);
    }

    @Override
    public Integer addById(User user) {
        return userMapper.insert(user);
    }

    @Override
    public List<User> findAll() {
        return userMapper.selectAll();
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRED, rollbackFor = {Exception.class})
    public void saveUser(User user) {
        userMapper.insertSelective(user);
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRED, rollbackFor = {Exception.class})
    public PageInfo<User> queryByPage(Integer curPage, Integer pageSize) {
        PageHelper.startPage(curPage, pageSize);
        List<User> userList = userMapper.selectAll();
        return new PageInfo<>(userList);
    }
}
